package com.zxl.grocery.microservice.crawler.pipeline;

import com.zxl.grocery.microservice.common.util.ToolUtil;
import com.zxl.grocery.microservice.crawler.entity.CrawlerFunny;
import com.zxl.grocery.microservice.crawler.service.biz.CrawlerFunnyBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Date;
import java.util.Map;

/**
 * @Author: Lzx
 * @Description: 笑话
 * @Date: Created in 11:55 2018/1/16
 * @Modified By:
 */
@Repository
public class FunnyPipeline implements Pipeline {
    @Autowired
    private CrawlerFunnyBiz crawlerFunnyBiz;

    @Override
    public void process(ResultItems resultItems, Task task) {
        for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
            if (entry.getKey().equals("funny")) {
                CrawlerFunny funny=(CrawlerFunny) entry.getValue();
                CrawlerFunny param = new CrawlerFunny();
                param.setTitle(funny.getTitle());
                CrawlerFunny funnies = crawlerFunnyBiz.selectOne(param);
                if (ToolUtil.isEmpty(funnies)) {//检查标题是否已存在
                    funny.setTypeId(1);
                    funny.setAuthor("zxl");
                    funny.setCreateDate(new Date());
                    funny.setStatus(1);
                    funny.setModifyDate(new Date());
                    crawlerFunnyBiz.insertSelective(funny);
                }
            }

        }
    }
}

